﻿@using System.Text;
@using WebMatrix.Data;
@{
           var service = UrlData[0].ToLower();;
           var id = UrlData[1];
           
        
         
            
        var data=new object[2];
        switch(service){
         
            case "upload":
                    string sheet=string.Empty;
                    string uploadPath=string.Empty;
                    string processPath=string.Empty;
                    bool isUpdated=false;
                    string message = string.Empty;
                    if(id.Equals("activation")){
                          sheet="Fns_Conv";
                          uploadPath="~/upload/Activation/";
                          processPath="~/Content/Activation/processed/";
                    }
                    
                    if(id.Equals("delivery")){
                          sheet="Delivery";
                          uploadPath="~/upload/Delivery/";
                          processPath="~/Content/Delivery/processed/";
                    }
                    
                    if(id.Equals("ccnos")){
                        sheet="Fns_Conv";
                        uploadPath="~/upload/CCNumbers/";
                        processPath="~/Content/CCNumbers/processed/";
                    }
                    
                     string filePath=string.Empty;
                     string testData=string.Empty;
                     int recProc=0;
                    List<Dictionary<string,string>> ccList=null;
                    try{
                        
                        filePath = UtilityManager.GetFileFromUploadDir(Server.MapPath(uploadPath));
                        //check if fileexists
                        if(UtilityManager.FileExists(filePath)){
                                //check type of upload and call method to extract data
                                if(id.Equals("activation") || id.Equals("ccnos")){
                                ccList = CashCardManager.ExtFromCIFXLS(filePath,sheet);    
                                }else{
                                ccList = CashCardManager.ExtFromDelXLS(filePath,sheet); 
                                }
                              
                                foreach(var d in ccList){
                                    
                                    string ccappno=d["CashCardAppNo"];  
                                    
                                    CustomerCashCard ccU=new CustomerCashCard();
                                    if(ccappno!=""){
                                      ccU = CashCardManager.GetCashCardDetails(ccappno);
                                      //increment no of records processed
                                       
                                    }
                                    if(ccU.CustomerID!=null){
                                     message+="\nCustomer ID:"+ccU.CustomerID;   
                                    var customerOld = CustomerManager.GetCustomer(ccU.CustomerID);
                                    Customer customerNew = new Customer();
                                    customerNew.CustomerID = customerOld.CustomerID;
                                    customerNew.Bdate = customerOld.Bdate;
                                    customerNew.PlaceOfBirth = customerOld.PlaceOfBirth;
                                    customerNew.CivilStatus = customerOld.CivilStatus;
                                    customerNew.IDType = customerOld.IDType;
                                    customerNew.IDNumber = customerOld.IDNumber;
                                    customerNew.CustomerStatus = customerOld.CustomerStatus;
                                    customerNew.SrcOfFunds=customerOld.SrcOfFunds;
                                    customerNew.Employer = customerOld.Employer;
                                    customerNew.NatureOfWork=customerOld.NatureOfWork;
                                    customerNew.DateUpdated = customerOld.DateUpdated; 
                                        
                                        
                                    //  @ObjectInfo.Print(ccU);  
                                    //set fields to upate based on id (activation,delivery,assign ccnumbers)
                                    //  if(ccU!=null){
                                    //message+="Ext Type:" + id;     
                                        if(String.Compare(id,"activation")==0){
                                              //  message+="Ext Type:" + id;
                                                ccU.CashCardStatusDate=DateTime.Now;
                                                ccU.CashCardStatus="ac";
                                                customerNew.CustomerStatus="Active";
                                                customerNew.DateUpdated=DateTime.Now;
                                                //update customer details
                                                CustomerManager.UpdateCustomerDetailsForCC(customerNew);
                                            }
                                            
                                            if(String.Compare(id,"delivery")==0){
                                               // message+="Remarks Value:" + d["Remarks"];  
                                                bool isDate=false;
                                                try{
                                                ccU.CardDelStatus=d["DelStatus"];
                                                if(d["DelDate"]!=""){
                                                  ccU.DelDate =Convert.ToDateTime(d["DelDate"]);    
                                                }     
                                                if(d["DelStatusDate"]!=""){
                                                  ccU.DelStatusDate = Convert.ToDateTime(d["DelStatusDate"]);    
                                                }
                                                 
                                                //ccU.Remarks=d["Remarks"];
                                                ccU.DelRemarks=d["Remarks"];
                                                ccU.CashCardStatus="far";  
                                                isDate=true;    
                                                }catch(Exception e){
                                                    
                                                    isDate=false;
                                                }
                                                
                                                
                                            }
                                            
                                            if(String.Compare(id,"ccnos")==0){
                                                ccU.CashCardNo = d["CashCardNo"];
                                                ccU.CashCardStatus="ip";
                                                ccU.CardDelStatus="fd";
                                                ccU.CashCardStatusDate = DateTime.Now;
                                                
                                            }
                                            ccU.DateUpdated = DateTime.Now;
                                            var r = CashCardManager.UpdateCustomerCC(ccU); 
                                    // }
                                    AuditManager.Log("upload",id,"","",@WebSecurity.CurrentUserName,ccU.CustomerID,ccU.CashCardNo);
                                    isUpdated=true;
                                    //update no of records processed
                                    recProc++;        
                                }
                                
                               
                                }
                        }else{
                            isUpdated=false;
                            message="File does not exist.";
                        } 
                    }catch(NullReferenceException nre){
                        message="Incorrect file uploaded for upload type";
                        isUpdated=false;
                        
                    }catch(Exception e){
                        message = e.ToString();
                        isUpdated=false;
                    }
                    
                   
                    
                    
                    //after processing move the file
                    if(isUpdated){
                        FileInfo f = new FileInfo(filePath);
                        UtilityManager.MoveFile(filePath,Server.MapPath(processPath)+f.Name);
                        UtilityManager.DeleteFile(filePath);
                        data[0]=string.Format("Cash card(s) updated successfully.<br/>Total records processed:#{0}",recProc) ;
                        //data[0]=message;
                    }else{
                        if(UtilityManager.FileExists(filePath)){
                             UtilityManager.DeleteFile(filePath);
                        }
                       
                        data[0]=message;
                    }
                   
                break;
               
            
               case "uploadfiles":
                     bool result=false;
                     try{
                         var fileData = Request.Files[0];
                        var destFolder = Request["folderPath"];
                        if(destFolder==""){
                            destFolder="/upload/Activation/";
                        }
                        var fileName = Path.GetFileName(fileData.FileName);
                        var fileSavePath = Server.MapPath(destFolder + fileName);
                        fileData.SaveAs(fileSavePath);
                        result=true;
                    }catch(Exception e){
                        throw e;
                    }
                    
                 
                    break;
            }
            
                      
             
             Json.Write(data,Response.Output);
    
}